package com.stsmobile;

import com.stsmobile.R;
import com.stsmobile.dataclass.Shipment;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class ShipmentHome extends Activity{
	private Button details, log;
	private static final CharSequence ERROR_CONNECTION = "Unable to connect\n" +
	"to the STS database.";
	private static final CharSequence ERROR_NOT_LOGGED_IN = "User is not logged in.";

	@Override
	public void onResume(){
		super.onResume();
		if(STSApplication.loggingOut()){
			finish();
		}
		if(STSApplication.toHome()){
			finish();
		}
	}

	@Override
	public void onCreate(Bundle a){
		super.onCreate(a);
		setContentView(R.layout.shipmenthome);

		Shipment s = STSApplication.getShipment();
		findAndSetText(R.id.shipmentID_w, " "+s.getShipment_id());
		findAndSetText(R.id.shippedOnText_w, " "+s.getShipDate());
		findAndSetText(R.id.shipStatusText_w, " "+s.getShipStatus());
		findAndSetText(R.id.receivedOnText_w, " "+s.getReceiveDate());
		findAndSetText(R.id.institutionText_w, " "+s.getInstitution());
		findAndSetText(R.id.labText_w, " "+s.getLab());

		details = (Button) findViewById(R.id.bdetails);
		details.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent nextpage = new Intent("com.stsmobile.ViewSpecimenData");
				startActivity(nextpage);
			}
		});
		log = (Button) findViewById(R.id.blog);
		log.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent nextpage = new Intent("com.stsmobile.LogSpecimenData");
				startActivity(nextpage);
			}
		});
	}

	private void findAndSetText(int id, String s){
		TextView txt = (TextView) findViewById(id);
		txt.setText(s);
	}

	private void makeXmlMessageToast(String result, int duration){
		CharSequence message="";

		//Get error message from server's response.
		int startMessage = result.indexOf("<msg>")+5;
		int endMessage = result.indexOf("</msg>");

		if(startMessage!=-1 && endMessage!=-1)
			message = result.substring(startMessage,endMessage);

		if(message.equals(""))
			message=ERROR_CONNECTION;

		if(message.equals("Sitekey Authentication Failed"))
			message=ERROR_NOT_LOGGED_IN;

		//Display Error Message to User in a Popup
		Context cur_context = getApplicationContext();
		Toast displaymessage = Toast.makeText(cur_context, message, duration);
		displaymessage.show();
	}

	private static final int MENU_LOGOUT = Menu.FIRST;
	private static final int MENU_HOME = Menu.FIRST + 1;

	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0,MENU_LOGOUT,0,"Logout");
		menu.add(0,MENU_HOME,0,"Home");
		return true;
	}

	public boolean onOptionsItemSelected(MenuItem item){
		switch(item.getItemId()){
		case MENU_LOGOUT:
			logout();
			return true;
		case MENU_HOME:
			home();
			return true;
		}
		return false;
	}

	public void logout(){
		String result = STSApplication.logout();
		makeXmlMessageToast(result, Toast.LENGTH_LONG);
		STSApplication.loggingOut(true);
		finish();
	}

	public void home(){
		STSApplication.toHome(true);
		finish();
	}


}


